package com.gzy.wbapplication.modules.log;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.gzy.wbapplication.R;
import com.gzy.wbapplication.events.ReloadLogEvent;
import com.gzy.wbapplication.manager.AuthManager;
import com.gzy.wbapplication.model.MyLog;
import com.gzy.wbapplication.model.Result;
import com.gzy.wbapplication.net.LogApi;
import com.gzy.wbapplication.net.core.XXModelCallback;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;

import okhttp3.Call;

public class EditLogActivity extends AppCompatActivity {
    MyLog myLog;

    EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_log);

        String log = getIntent().getStringExtra("log");
        myLog = JSON.parseObject(log, MyLog.class);


        findViewById(R.id.rl_back).setOnClickListener((v)->{
            finish();
        });

        findViewById(R.id.tv_commit).setOnClickListener((v)->{
            commit();
        });findViewById(R.id.tv_del).setOnClickListener((v)->{
            del();
        });

        editText = findViewById(R.id.editText);
        editText.setText(myLog.getContent());
    }

    private void del() {
        LogApi.delLog(AuthManager.loadToken(getApplicationContext()), myLog.getId(), new XXModelCallback<Result<String>>(new TypeReference<Result<String>>(){}) {
            @Override
            public void onResponseData(Call call, Result<String> model) {
                Toast.makeText(getApplicationContext(), "删除成功", Toast.LENGTH_SHORT).show();
                EventBus.getDefault().post(new ReloadLogEvent());
                finish();
            }

            @Override
            public void onFailure2(Call call, IOException e, ErrType type, String message) {
                Toast.makeText(getApplicationContext(), "删除失败：" + message, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void commit() {
        String text = editText.getText().toString();
        if (TextUtils.isEmpty(text)) {
            Toast.makeText(this, "日志内容不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        LogApi.updateLog(AuthManager.loadToken(this),myLog.getId(), text, new XXModelCallback<Result<MyLog>>(new TypeReference<Result<MyLog>>(){}) {
            @Override
            public void onResponseData(Call call, Result<MyLog> model) {
                Toast.makeText(getApplicationContext(), "修改成功", Toast.LENGTH_SHORT).show();
                EventBus.getDefault().post(new ReloadLogEvent());
                finish();
            }

            @Override
            public void onFailure2(Call call, IOException e, ErrType type, String message) {
                Toast.makeText(getApplicationContext(), "修改失败：" + message, Toast.LENGTH_SHORT).show();
            }
        });
    }
}
